package com.xcf.mybatis_example.dao.mapper;

import com.xcf.mybatis_example.dao.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("select * from user where id = #{id}")
    User findOne(User user);

    @Select("select * from user where username = #{username}")
    List<User> findByUsername(User user);

    @Select("select * from user where username = #{username} limit 1")
    User findTopByUsername(User user);

    @Select("select * from user where username = #{username} limit 1 for update")
    User findCCTopByUsername(User user);

//    @Transactional
    @Insert("INSERT INTO `user` (`username`, `num`,`update_time`) VALUES (#{username}, #{num}, #{update_time})")
    void save(User user);

//    @Transactional
    @Update("update user set num = num + #{num}, update_time = CURRENT_TIMESTAMP  where username = #{username}")
    void update(User user);
}
